package com.hrt.datasender.process.strategy.hj212;

import com.btcode.db.result.Row;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.service.BdSendRecordService;
import com.hrt.datasender.datastorage.StorageDataType;
import com.hrt.datasender.process.strategy.hj212.util.DataType;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

public class Com2011UdpProcess extends ProcessStrategyMonitorData {

    @Override
    public String getStrategyName() {
        return "中山UDP实时数据";
    }

    @Override
    public DataType getDataType() {
        return DataType.second;
    }

    @Override
    public String getCommandId() {
        return "2011";
    }

    @Override
    public Object packet(List<Row> data, BcSendPlatformModel sendPlatform) {
        final long timeDiffer = 10000;
        String msg = getCommandPacket(sendPlatform, getDataType(), data, timeDiffer);
        try {
            long date = (long) data.get(0).get("DATATIME");
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(date);
            StorageDataType dataType = StorageDataType.SECOND;
            String ip = sendPlatform.getIp();
            String port = sendPlatform.getPort();
            BdSendRecordService.getInstance().updateLastSendDate(dataType, ip, port, cal.getTime());
        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return msg;
    }

    /**
     * 示例报文：ST:00001;10/04/28 15:03;K1:1;K2:1;K6:0;A1:26.00;A2:30.00;A3:0.000;
     * A4:0.000;A5:0.315;A6:0.000;V:10.9;H:018.
     * ST:00001代表站点编码，10/04/28 15:03代表发送时间，A1~A6:氨氮、ss、PH、溶解氧、电导率、水温
     * @param platform
     * @param dataType
     * @param data
     * @param timeDiffer
     * @return
     */
    @Override
    public String getCommandPacket(BcSendPlatformModel platform, DataType dataType, List<Row> data, long timeDiffer) {
        SimpleDateFormat sf = new SimpleDateFormat("yy/MM/dd HH:mm");
        StringBuilder strBuilder = new StringBuilder();

        if (data.size() < 1) {
            return null;
        }

        long date = (long) data.get(0).get("DATATIME");
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date);
        String dataTime = sf.format(cal.getTime());

        strBuilder.append("ST:" + platform.getMn() + ";");
        strBuilder.append(dataTime + ";");
        for (Row row : data) {
            String pramCodeBus = String.valueOf(row.get("PARAM_CODE_BUS"));
            String measRe = String.valueOf(row.get("MEAS_RE"));
            //去除科学计数法
            BigDecimal measBD = new BigDecimal(measRe);
            measRe = measBD.toString();
            strBuilder.append(pramCodeBus + ":" + measRe + ";");
        }
        String msg = strBuilder.toString();
        msg = msg.substring(0,strBuilder.length()-1);
        msg += ".";

        return msg;
    }
}
